package com.myapp.ruyixing.ui.shopmanager;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.myapp.ruyixing.R;
import com.myapp.ruyixing.http.RYHttpClient;
import com.myapp.ruyixing.http.RYHttpResponseHandler;
import com.myapp.ruyixing.model.Model;
import com.myapp.ruyixing.ui.BaseActivity;
import com.myapp.ruyixing.ui.widget.TitleBar;
import com.myapp.ruyixing.utils.LoginHelper;

public class OrderNotifyUserActivity extends BaseActivity {

	private String mName;
	private String mPhone;
	private String mOrderId;

	private TitleBar mTitleBar;
	private View mLoadingView;

	private TextView mNameView;
	private TextView mPhoneView;

	private RYHttpResponseHandler mHttpResponseHandler = new RYHttpResponseHandler() {

		@Override
		public void onSuccess(Object o) {
			try {
				Model resul = (Model) o;
				onLoadingDataFinished(true, resul);
			} catch (Exception e) {
				onLoadingDataFinished(false, null);
			}
		}

		@Override
		public void onFailure(Throwable throwable) {
			onLoadingDataFinished(false, null);
		}
	};

	private void onLoadingDataStarted(String shopbduss, String orderid,
			String name, String phone, String content) {
		showLoading();
		RYHttpClient.sendSMS(this, mHttpResponseHandler, shopbduss, orderid,
				name, phone, content);
	}

	private void onLoadingDataFinished(boolean succeed, Model result) {
		hideLoading();
		if (succeed && result != null && result.errno == 0) {
			Toast.makeText(this, result.msg, Toast.LENGTH_SHORT).show();
			finish();
		} else {
			Toast.makeText(this, "发送失败:" + (result == null ? "" : result.msg),
					Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);

		setContentView(R.layout.activity_shop_manager_order_notify_user);

		initView();

		mOrderId = getIntent().getStringExtra("orderid");

		mName = getIntent().getStringExtra("name");
		mPhone = getIntent().getStringExtra("phone");
		mNameView.setText("姓名：" + mName);
		mPhoneView.setText("电话：" + mPhone);
	}

	private void initView() {
		mTitleBar = (TitleBar) findViewById(R.id.title_bar);
		mLoadingView = findViewById(R.id.my_loading);
		mNameView = (TextView) findViewById(R.id.name);
		mPhoneView = (TextView) findViewById(R.id.phone);
		mTitleBar.setOnIconClickedListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				onBackPressed();
			}
		});

		findViewById(R.id.btn_commit).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String content = ((EditText) findViewById(R.id.message))
						.getText().toString().trim();

				if (TextUtils.isEmpty(content)) {
					Toast.makeText(OrderNotifyUserActivity.this, "请输入短信内容！",
							Toast.LENGTH_SHORT).show();
					return;
				}

				onLoadingDataStarted(LoginHelper.getInstance().getShopBduss(),
						mOrderId, mName, mPhone, content);
			}
		});
	}

	private void showTitleBarLoading() {
		mTitleBar.showLoading(true);
	}

	private void hideTitleBarLoading() {
		mTitleBar.showLoading(false);
	}

	private void showLoading() {
		mLoadingView.setVisibility(View.VISIBLE);
	}

	private void hideLoading() {
		mLoadingView.setVisibility(View.GONE);
	}
}
